using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.Xna.Framework;

using HadronData;

namespace HadronEditor
{
    public partial class EntityPropertiesForm : Form
    {
        private Level m_level;
        private EntityMarker m_entityMarker;

        private void m_btnOK_Click(object p_objSender, EventArgs p_eventArgs)
        {
            bool bError = false;

            // entity name
            if (m_txtName.Text.Length == 0)
            {
                bError = true;
                m_errorProvider.SetError(m_txtName, "No entity name specified");
            }
            else
            {
                EntityMarker entityMarker = m_level.GetEntityMarker(m_txtName.Text);
                if (entityMarker != null && entityMarker != m_entityMarker)
                {
                    bError = true;
                    m_errorProvider.SetError(m_txtName, "Duplicate entity name");
                }
            }

            // position
            Vector2 vecPosition = m_entityMarker.Position;
            if (!float.TryParse(m_txtPositionX.Text, out vecPosition.X))
            {
                bError = true;
                m_errorProvider.SetError(m_txtPositionX, "Invalid position X coordinate");
            }
            if (!float.TryParse(m_txtPositionY.Text, out vecPosition.Y))
            {
                bError = true;
                m_errorProvider.SetError(m_txtPositionY, "Invalid position Y coordinate");
            }

            // orientation
            float fOrientation = MathHelper.ToDegrees(m_entityMarker.Orientation);
            if (!float.TryParse(m_txtOrientation.Text, out fOrientation))
            {
                bError = true;
                m_errorProvider.SetError(m_txtOrientation, "Invalid orientation");
            }

            if (!bError)
            {
                m_entityMarker.Name = m_txtName.Text;
                m_entityMarker.EntityType = (EntityType)Enum.Parse(
                    typeof(EntityType), m_cmbEntityType.SelectedItem.ToString());
                m_entityMarker.Position = vecPosition;
                m_entityMarker.Orientation = MathHelper.ToRadians(fOrientation);

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        public EntityPropertiesForm(Level p_level, EntityMarker p_entityMarker)
        {
            InitializeComponent();

            m_level = p_level;
            m_entityMarker = p_entityMarker;
            m_txtName.Text = m_entityMarker.Name;
            m_txtPositionX.Text = ((int)m_entityMarker.Position.X).ToString();
            m_txtPositionY.Text = ((int)m_entityMarker.Position.Y).ToString();
            m_txtOrientation.Text = ((int)MathHelper.ToDegrees(m_entityMarker.Orientation)).ToString();
        }

        private void EntityPropertiesForm_Load(object p_objSender, EventArgs p_eventArgs)
        {
            // populate entity type
            foreach (EntityType entityType in Enum.GetValues(typeof(EntityType)))
                m_cmbEntityType.Items.Add(entityType.ToString());
            for (int nIndex = 0; nIndex < m_cmbEntityType.Items.Count; nIndex++)
                if (m_cmbEntityType.Items[nIndex].ToString() == m_entityMarker.EntityType.ToString())
                    m_cmbEntityType.SelectedIndex = nIndex;
        }
    }
}